<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;


use think\Log;

class Wu extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();

    }

    //http://wu.test/api/wu/createprocedures
    //生成所有产品的工序
    public function createprocedures()
    {

        $list = \app\admin\model\wu\Productspec::all();
        foreach ($list as $row) {

            $procedure = new \app\admin\model\wu\Procedure;

            if ($row->spectype_id == 1) {
                //半成品  到廠、刀柄雷標、段差、半成品庫存
                $procedureList = [
                    ['productspec_id' => $row->id, 'name' => '到廠', 'typenumber'=> '01', 'order'=>1 ],
                    ['productspec_id' => $row->id, 'name' => '刀柄雷標', 'typenumber'=> '02', 'order'=>2 ],
                    ['productspec_id' => $row->id, 'name' => '段差', 'typenumber'=> '03', 'order'=>3 ],
                    ['productspec_id' => $row->id, 'name' => '半成品庫存', 'typenumber'=> '04', 'order'=>4 ],
                ];

            } else if ($row->spectype_id == 2) {
                //成品(白刀)
                $procedureList = [
                    ['productspec_id' => $row->id, 'name' => '半成品庫存', 'typenumber'=> '04', 'order'=>1],
                    ['productspec_id' => $row->id, 'name' => '開刃', 'typenumber'=> '05', 'order'=>2],
                    ['productspec_id' => $row->id, 'name' => '白刀庫存', 'typenumber'=> '06', 'order'=>3],
//                    ['productspec_id' => $row->id, 'name' => '送鍍', 'typenumber'=> '07', 'order'=>4],
                    ['productspec_id' => $row->id, 'name' => '檢驗', 'typenumber'=> '08', 'order'=>4],
                    ['productspec_id' => $row->id, 'name' => '包裝貼標', 'typenumber'=> '09', 'order'=>5],
                    ['productspec_id' => $row->id, 'name' => '回公司入庫', 'typenumber'=> '10', 'order'=>6],
                ];

            } else if ($row->spectype_id == 3) {
                //成品(球刀/銑刀)
                $procedureList = [
                    ['productspec_id' => $row->id, 'name' => '半成品庫存', 'typenumber'=> '04', 'order'=>1],
                    ['productspec_id' => $row->id, 'name' => '開刃', 'typenumber'=> '05', 'order'=>2],
                    ['productspec_id' => $row->id, 'name' => '白刀庫存', 'typenumber'=> '06', 'order'=>3],
                    ['productspec_id' => $row->id, 'name' => '送鍍', 'typenumber'=> '07', 'order'=>4],
                    ['productspec_id' => $row->id, 'name' => '檢驗', 'typenumber'=> '08', 'order'=>5],
                    ['productspec_id' => $row->id, 'name' => '包裝貼標', 'typenumber'=> '09', 'order'=>6],
                    ['productspec_id' => $row->id, 'name' => '回公司入庫', 'typenumber'=> '10', 'order'=>7],
                ];
            }  else if ($row->spectype_id == 4) {
                //調撥單(白刀成品)
                $procedureList = [
                    ['productspec_id' => $row->id, 'name' => '白刀庫存', 'typenumber'=> '06', 'order'=>1],
                    ['productspec_id' => $row->id, 'name' => '檢驗', 'typenumber'=> '08', 'order'=>2],
                    ['productspec_id' => $row->id, 'name' => '包裝貼標', 'typenumber'=> '09', 'order'=>3],
                    ['productspec_id' => $row->id, 'name' => '回公司入庫', 'typenumber'=> '10', 'order'=>4],
                ];
            }

            $procedure->saveAll($procedureList);

        }

        $this->success("ok");
    }


    //http://wu.test/api/wu/movewxprocedure
    //去无效的制程
    public function movewxprocedure()
    {
        $list = \app\admin\model\wu\Procedure::all();
        foreach ($list as $row) {
            $ps = \app\admin\model\wu\Productspec::get($row->productspec_id);
            if (!$ps) {
                $row->delete();
            }
        }
        $this->success("ok");
    }

/*
 *  //去重复的产品
DELETE
FROM fa_wu_productspec
WHERE name IN(SELECT
               name
             FROM (SELECT
                     name
                   FROM fa_wu_productspec
                   GROUP BY name
                   HAVING COUNT(name) > 1) a)
    AND id NOT IN(SELECT
                    *
                  FROM (SELECT
                          MIN(id)
                        FROM fa_wu_productspec
                        GROUP BY name
                        HAVING COUNT(name) > 1) b)

 */

    //添加產品规格编号
    //http://wu.test/api/wu/addspecnumber
    public function addspecnumber()
    {
        $list = \app\admin\model\wu\Productspec::all();
        foreach ($list as &$row) {
            if (strpos($row->name, '白刀')!== false ) {
                //WU39/2BES-R0.1-H0.3-L45 微小徑球刀
                $row->spectype_id = 2;
                $row->specnumber = 'WU39-02-'.$row->id;
            } else if (strpos($row->name, '銑刀')!== false|| strpos($row->name, '球刀')!== false) {
                $row->spectype_id = 3;
                $row->specnumber = 'WU39-03-'.$row->id;
            } else {
                //段差圓棒
                $row->spectype_id = 1;
                $row->specnumber = 'WU39-01-'.$row->id;
            }

            $row->save();
        }
        $this->success("ok");
    }

    //添加工序编号
    //http://wu.test/api/wu/addprocedurenumber
    public function addprocedurenumber()
    {
        $list = \app\admin\model\wu\Productspec::all();
        foreach ($list as $row) {
            $list2 = \app\admin\model\wu\Procedure::where('productspec_id', $row->id)->select();
            $index = 0;
            foreach ($list2 as &$row2) {
                $index++;
                $row2->procedurenumber = $row->specnumber.'-'.$row2->typenumber;
                $row2->order = $index;
                $row2->save();
            }
        }
        $this->success("ok");
    }

    //添加产品BOM表
    //http://wu.test/api/wu/addBomproductspec
    public function addBomproductspec()
    {
        $list = \app\admin\model\wu\Productspec::all();
        foreach ($list as $row) {
            $bom = \app\admin\model\wu\Bomproductspec::where('productspec_id', $row->id)->find();
            if (!$bom) {
                $bom = new \app\admin\model\wu\Bomproductspec;
                $bom->productspec_id = $row->id;
                $bom->name = $row->lycl;
                $bom->save();
            }
        }
        $this->success("ok");
    }

    //初始化产品库存表
    //http://wu.test/api/wu/addStockproductspec
    public function addStockproductspec()
    {
        $list = \app\admin\model\wu\Productspec::all();
        foreach ($list as $row) {
            $stock = \app\admin\model\wu\Stockproductspec::where('productspec_id', $row->id)->find();
            if (!$stock) {
                $stock = new \app\admin\model\wu\Stockproductspec;
                $stock->id = $row->id;
                $stock->productspec_id = $row->id;
                $stock->num = 0;
                $stock->minnum = 100;
                $stock->save();
            }
//            $stock = new \app\admin\model\wu\Stockproductspec;
//            $stock->id = $row->id;
//            $stock->productspec_id = $row->id;
//            $stock->num = 0;
//            $stock->minnum = 100;
//            $stock->save();
        }
        $this->success("ok");
    }

    //http://wu.test/api/wu/login?username=admin&password=123456
    public function login()
    {
        $username = $this->request->request('username');
        $password = $this->request->request('password');
        if (!$username || !$password) {
            $this->error(__('Invalid parameters'));
        }

        $adminuser = \app\admin\model\Admin::where("username", $username)->find();
        if ($adminuser) {
            if ($adminuser->password != md5(md5($password) . $adminuser->salt)) {
                $this->error('Password is incorrect');
            }

            $data = [
                'adminid' => $adminuser->id,
            ];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error("错误的用户名");
        }
    }

    //http://wu.test/api/wu/test
    public function test()
    {
        $model = new \app\admin\model\wu\Procedurestatelog;
        $list = $model
            ->with(['jt', 'productspec', 'workorder', 'procedure', 'procedurestate', 'man'])
            ->select();
        foreach ($list as &$row) {
            $procedurestate = \app\admin\model\wu\Procedurestate::get($row->procedurestate_id);
            if (!$procedurestate) {
                echo "<br>procedurestate--".$row->id;
                continue;
            }
            $row->procedure_id = $procedurestate->procedure_id;
            $planlog = \app\admin\model\wu\Planlog::get($procedurestate->planlog_id);
            if (!$planlog) {
                echo "<br> planlog--".$row->id;
                continue;
            }
            $row->workorder_id = $planlog->workorder_id;
            $procedure = \app\admin\model\wu\Procedure::get($row->procedure_id);
            if (!$procedure) {
                echo "<br> procedure--".$row->id;
                continue;
            }
            $row->productspec_id = $procedure->productspec_id;
            $row->save();
        }

        echo "<br>OK";

    }

    //http://wu.test/api/wu/cleardata
    //定期清理数据 保留最近的1百万条记录
    public function cleardata()
    {
        $hjdevicelog = \app\admin\model\wu\Hjdevicelog::order('id desc')->limit(1)->find();
        if ($hjdevicelog) {
            \app\admin\model\wu\Hjdevicelog::where('id', '<', $hjdevicelog->id - 1000000)->delete();
        }

        $sql = "select id from test order by id desc limit 1";
        $one = Db::query($sql);
        if ($one) {
            $lastId = $one[0]['id'] - 10000;
            if ($lastId > 0) {
                $sql = "DELETE FROM `test` WHERE  `id` < {$lastId}";
                Db::query($sql);
            }
        }
        echo "OK";
    }


    //http://wu.test/api/wu/cjjkdata
    public function cjjkdata()
    {
        $wd = \app\admin\model\wu\Hjdevicelog::where("commandtype", "FE00")
            ->where('hjdevice_id', 2)
            ->order('id desc')
            ->limit(1)
            ->find();

        $sd = \app\admin\model\wu\Hjdevicelog::where("commandtype", "FE01")
            ->where('hjdevice_id', 2)
            ->order('id desc')
            ->limit(1)
            ->find();

        $from = date("Y-m-d H:i:s", time() - 10);//10秒沒有值就歸零
        $md4 = \app\admin\model\wu\Hjdevicelog::where("commandtype", "FE04")
            ->where('hjdevice_id', 3)
            ->field("value1, value2, value3")
            ->where('create_time', '>', $from)
            ->order('id desc')
            ->limit(1)
            ->find();
        $md5 = \app\admin\model\wu\Hjdevicelog::where("commandtype", "FE05")
            ->where('hjdevice_id', 3)
            ->field("value1, value2, value3")
            ->where('create_time', '>', $from)
            ->order('id desc')
            ->limit(1)
            ->find();
        $md6 = \app\admin\model\wu\Hjdevicelog::where("commandtype", "FE06")
            ->where('hjdevice_id', 3)
            ->field("value1, value2, value3")
            ->where('create_time', '>', $from)
            ->order('id desc')
            ->limit(1)
            ->find();
        $md7 = \app\admin\model\wu\Hjdevicelog::where("commandtype", "FE07")
            ->where('hjdevice_id', 3)
            ->field("value1, value2, value3")
            ->where('create_time', '>', $from)
            ->order('id desc')
            ->limit(1)
            ->find();

        if ($md4) {
            $md4 = [$md4->value1, $md4->value2, $md4->value3];
        } else {
            $md4 = [0, 0, 0];
        }

        if ($md5) {
            $md5 = [$md5->value1, $md5->value2, $md5->value3];
        } else {
            $md5 = [0, 0, 0];
        }

        if ($md6) {
            $md6 = [$md6->value1, $md6->value2, $md6->value3];
        } else {
            $md6 = [0, 0, 0];
        }

        if ($md7) {
            $md7 = [$md7->value1, $md7->value2, $md7->value3];
        } else {
            $md7 = [0, 0, 0];
        }

        //手动操作变为正常绿色; 如果没有恢复正常20秒后会继续FE08
        //所以只需查询20秒内的记录
        $from = date("Y-m-d H:i:s", time() - 20);
        $mdstate1 = \app\admin\model\wu\Hjdevicelog::where("commandtype", "FE08")
            ->where('value1', 0)
            ->where('create_time', '>', $from)
            ->where('hjdevice_id', 3)
            ->order('id desc')
            ->limit(1)
            ->find();

        if ($mdstate1) {
            $mdstate1 = 1;// 异常
        } else {
            $mdstate1 = 0;// 正常
        }

        $mdstate2 = \app\admin\model\wu\Hjdevicelog::where("commandtype", "FE08")
            ->where('value1', 1)
            ->where('create_time', '>', $from)
            ->where('hjdevice_id', 3)
            ->order('id desc')
            ->limit(1)
            ->find();

        if ($mdstate2) {
            $mdstate2 = 1;// 异常
        } else {
            $mdstate2 = 0;// 正常
        }

        $mdstate3 = \app\admin\model\wu\Hjdevicelog::where("commandtype", "FE08")
            ->where('value1', 2)
            ->where('create_time', '>', $from)
            ->where('hjdevice_id', 3)
            ->order('id desc')
            ->limit(1)
            ->find();

        if ($mdstate3) {
            $mdstate3 = 1;// 异常
        } else {
            $mdstate3 = 0;// 正常
        }

        $mdstate4 = \app\admin\model\wu\Hjdevicelog::where("commandtype", "FE08")
            ->where('value1', 3)
            ->where('create_time', '>', $from)
            ->where('hjdevice_id', 3)
            ->order('id desc')
            ->limit(1)
            ->find();

        if ($mdstate4) {
            $mdstate4 = 1;// 异常
        } else {
            $mdstate4 = 0;// 正常
        }


        $from = date("Y-m-d H:i:s", time() - 10);
        $ycgj = "接收裝置正常運作中";
        //FE09 --- 10 秒無異常命令，則顯示”接收裝置正常運作中”
        $state9 = \app\admin\model\wu\Hjdevicelog::where("commandtype", "FE09")
            ->where('create_time', '>', $from)
            ->where('hjdevice_id', 3)
            ->order('id desc')
            ->limit(1)
            ->find();

        if ($state9) {
            $value = intval($state9->value1);
            switch ($value) {
                default:
                    break;
                case 0;
                    $ycgj = "溢位裝置 A 異常";
                    break;
                case 1;
                    $ycgj = "溢位裝置 B 異常";
                    break;
                case 2;
                    $ycgj = "馬達量測電流裝置 A-1 異常";
                    break;
                case 3;
                    $ycgj = "馬達量測電流裝置 A-2 異常";
                    break;
                case 4;
                    $ycgj = "馬達量測電流裝置 B-1 異常";
                    break;
                case 5;
                    $ycgj = "馬達量測電流裝置 B-2 異常";
                    break;
            }

        }

        $start = date('Y-m-d H:i:s', time() - 600);//最近 10 分鐘
        $end = date('Y-m-d H:i:s', time());
        //循環過濾油箱
        $mdlist1 = \app\admin\model\wu\Hjdevicelog::where("commandtype = 'FE04' or commandtype = 'FE05'")
            ->where('hjdevice_id', 3)
            ->where('create_time', '>=', $start)
            ->where('create_time', '<=', $end)
            ->order('id desc')
            ->select();

        //第一集油箱
        $mdlist2 = \app\admin\model\wu\Hjdevicelog::where("commandtype = 'FE06' or commandtype = 'FE07'")
            ->where('hjdevice_id', 3)
            ->where('create_time', '>=', $start)
            ->where('create_time', '<=', $end)
            ->order('id desc')
            ->select();

        $md1list1 = [];
        $md1list2 = [];
        $md1list3 = [];
        $md1list4 = [];
        $md1list5 = [];
        $md1list6 = [];
        $md1Xlist = [];
        foreach ($mdlist1 as $row1) {
            $md1Xlist[] = $row1['create_time'];

            if ($row1->commandtype == 'FE04') {
                $md1list1[] = $row1['value1'];
                $md1list2[] = $row1['value2'];
                $md1list3[] = $row1['value3'];
                $md1list4[] = 0;
                $md1list5[] = 0;
                $md1list6[] = 0;
            } else {
                $md1list1[] = 0;
                $md1list2[] = 0;
                $md1list3[] = 0;
                $md1list4[] = $row1['value1'];
                $md1list5[] = $row1['value2'];
                $md1list6[] = $row1['value3'];
            }
        }

        $md2list1 = [];
        $md2list2 = [];
        $md2list3 = [];
        $md2list4 = [];
        $md2list5 = [];
        $md2list6 = [];
        $md2Xlist = [];
        foreach ($mdlist2 as $row2) {
            $md2Xlist[] = $row2['create_time'];

            if ($row2->commandtype == 'FE06') {
                $md2list1[] = $row2['value1'];
                $md2list2[] = $row2['value2'];
                $md2list3[] = $row2['value3'];
                $md2list4[] = 0;
                $md2list5[] = 0;
                $md2list6[] = 0;
            } else {
                $md2list1[] = 0;
                $md2list2[] = 0;
                $md2list3[] = 0;
                $md2list4[] = $row2['value1'];
                $md2list5[] = $row2['value2'];
                $md2list6[] = $row2['value3'];
            }
        }

        $data = array(
            'md1list1' => array_reverse($md1list1),
            'md1list2' => array_reverse($md1list2),
            'md1list3' => array_reverse($md1list3),
            'md1list4' => array_reverse($md1list4),
            'md1list5' => array_reverse($md1list5),
            'md1list6' => array_reverse($md1list6),
            'md1Xlist' => array_reverse($md1Xlist),

            'md2list1' => array_reverse($md2list1),
            'md2list2' => array_reverse($md2list2),
            'md2list3' => array_reverse($md2list3),
            'md2list4' => array_reverse($md2list4),
            'md2list5' => array_reverse($md2list5),
            'md2list6' => array_reverse($md2list6),
            'md2Xlist' => array_reverse($md2Xlist),
            'ycgj' => $ycgj,
            'mdstate1' => $mdstate1,
            'mdstate2' => $mdstate2,
            'mdstate3' => $mdstate3,
            'mdstate4' => $mdstate4,

            'wdvalue' => $wd->value1,
            'sdvalue' => $sd->value1,
            'md4' => $md4,
            'md5' => $md5,
            'md6' => $md6,
            'md7' => $md7,
        );

        $this->success('ok', $data);

    }

}





